package com.digitalchina.lxnsy.activity.farmTrain;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.webkit.JavascriptInterface;

import com.digitalchina.lxnsy.api.BusinessFile;
import com.digitalchina.lxnsy.base.BaseHtmlActivity;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.entity.FileVO;
import com.digitalchina.lxnsy.loader.GlideImageLoader;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.GotoUtil;
import com.lzy.imagepicker.ImagePicker;
import com.lzy.imagepicker.bean.ImageItem;
import com.lzy.imagepicker.ui.ImageGridActivity;

import java.util.ArrayList;

/**
 * ================================================
 * 项目名称：android
 * 类描述：机构信息维护
 * 创建人：yzm
 * 创建时间：2020/1/8 17:26
 * 修改备注：
 * ================================================
 */
public class TrainOrgUpdateActivity extends BaseHtmlActivity {

    public static final int REQUEST_CODE = 101;

    //图片
    public static final int REQUEST_CODE_SELECT = 100;
    private ArrayList<ImageItem> selectedImageList = new ArrayList<>(); //当前选择的所有图片
    private String filePath = ""; // 图片地址

    @Override
    protected void loadHtml() {
        webView.addJavascriptObject(new JsApi(), null);
        loadUrl("train-org-update");
        setTitle("机构信息");

        ImagePicker imagePicker = ImagePicker.getInstance();
        imagePicker.setImageLoader(new GlideImageLoader());   //设置图片加载器
        imagePicker.setShowCamera(true);  //显示拍照按钮
        imagePicker.setSelectLimit(1);    //选中数量限制
        imagePicker.setMultiMode(false);    //是否多选模式
        imagePicker.setCrop(false);

    }

    @Override
    public void rightOnClick() {
    }

    @Override
    public void setHandler() {
        super.setHandler();
        mHandler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message message) {
                dismissDialog();
                switch (message.what) {
                    case MSG.MSG_FILE_UPLOAD_SUCCESS:  // 上传图片
                        FileVO fileVO = FastJsonUtil.getBean(message.obj.toString(), "body", "fileStorage", FileVO.class);
                        if (fileVO != null) {
                            callJsMethod("setImg", new String[]{fileVO.getName()});
                        }
                        break;
                    case MSG.MSG_FILE_UPLOAD_FIELD:
                        showToast(message.obj.toString());
                        break;

                }
                return false;
            }
        });
    }


    public class JsApi {

        //保存
        @JavascriptInterface
        public void saveClose(Object msg) {
            finish();
            //TODO yang 刷新登录信息
        }

        //选择图片
        @JavascriptInterface
        public void selectImg(Object msg) {
            Intent intent = new Intent(activity, ImageGridActivity.class);
            startActivityForResult(intent, REQUEST_CODE_SELECT);
        }

        //选择擅长培训类型
        @JavascriptInterface
        public void selectTrainType(Object msg) {
            GotoUtil.gotoForResultActivity(activity, OrgSelectTypeActivity.class, REQUEST_CODE);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == ImagePicker.RESULT_CODE_ITEMS) {
            //添加图片返回
            if (data != null && requestCode == REQUEST_CODE_SELECT) {
                showDialog("上传中，请稍候");
                ArrayList<ImageItem> images = (ArrayList<ImageItem>) data.getSerializableExtra(ImagePicker.EXTRA_RESULT_ITEMS);
                filePath = images.get(0).path;
                selectedImageList.addAll(images);
                BusinessFile.imageCompressAndUpload(activity, filePath, mHandler);
            }

        } else if (resultCode == RESULT_OK) {
            //选择培训类型返回
            callJsMethod("showTrainType");
        }
    }

}
